/*
   Copyright 2011 
   
   DIFED

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package difed.restaurante.util;

import difed.restaurante.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager.NameNotFoundException;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

 
public class Notes {
    
    private static final String NOTES_PREF = "notes";
    
    public static void show(final Activity activity) {
        final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
        int savedVersion = preferences.getInt(NOTES_PREF, 0);
        final int actualVersion = getVersion(activity);
        
        if (savedVersion < actualVersion) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setTitle(R.string.changelog_title);
            builder.setCancelable(false);
            builder.setPositiveButton(R.string.si, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    accept(preferences, NOTES_PREF, actualVersion);                    
                }
            });            
            builder.setView(readNotes(activity));
            builder.create().show();
        }
    }

    private static int getVersion(Activity activity) {
        int v = 0;
        try {
            v = activity.getPackageManager().getPackageInfo(activity.getApplicationInfo().packageName, 0).versionCode;
        } catch (NameNotFoundException e) {
            // Huh? Really?
        }
        return v;
    }

    private static String getVersionName(Activity activity) {
        String v = "-";
        try {
            v = activity.getPackageManager().getPackageInfo(activity.getApplicationInfo().packageName, 0).versionName;
        } catch (NameNotFoundException e) {
            // Huh? Really?
        }
        return v;
    }

    private static void accept(SharedPreferences preferences, String key, int value) {
        Editor editor = preferences.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    private static View readNotes(Activity activity) {
        LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = vi.inflate(R.layout.popup, null);
        LinearLayout layout = (LinearLayout) view.findViewById(R.id.popup_main);
        TextView text = new TextView(activity);
        MarginLayoutParams layoutParams = new MarginLayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        text.setLayoutParams(layoutParams);
        text.setText(activity.getString(R.string.version) + " " + getVersionName(activity));
        layout.addView(text);
        String[] lines = activity.getResources().getStringArray(R.array.notes);
        for (int i = 0; i < lines.length; i++) {
            text = new TextView(activity);
            layoutParams = new MarginLayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(0, 20, 0, 20);
            text.setLayoutParams(layoutParams);
            text.setText(lines[i]);
            layout.addView(text);
        }
        return view;
    }

}
